//
//  BaseView.swift
//  SwiftDemo
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 yimiSun. All rights reserved.
//

/**
 * 创建UI
 */
import Foundation
import YYKit

/** BaseView */
class BaseView: UIView,ViewProtocol {
    
    typealias MenuItemClick = ((_ menu:String?,_ index:Int) -> (Void))?
    typealias BackViewClick = (() -> (Void))?
    
    
    //MARK: -
    init(ViewModel vm: ViewModelProtocol) {
        super.init(frame: CGRect.zero)
        
        self.setupViews()
        self.bindViewModel()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func bindViewModel() {
        
    }
    
    func setupViews() {
        
    }
    
    
    //MARK: - 公共方法
    /**
     * 创建按钮
     * @return UIButton
     */
    static func createBtn(rect:CGRect,
                          strTitle:String? = nil,
                          titleColor:UIColor? = nil,
                          txtFont:UIFont? = nil,
                          image:UIImage?,
                          backgroundColor:UIColor? = UIColor.clear,
                          borderColor:UIColor? = nil,
                          cornerRadius:CGFloat? = CGFloat.init(5),
                          isRadius:Bool = false,
                          backgroundImage:UIImage? = nil,
                          borderWidth:CGFloat? = nil)->UIButton{
        let btnTemp = UIButton.init(type: .custom)
        btnTemp.showsTouchWhenHighlighted = true
        
        btnTemp.frame = rect
        btnTemp.titleLabel?.font = txtFont
        
        if backgroundColor != nil {
            btnTemp.backgroundColor = backgroundColor
        }
        
        if strTitle != nil {
            btnTemp.setTitle(strTitle, for: .normal)
        }
        
        if titleColor != nil {
            btnTemp.setTitleColor(titleColor, for: .normal)
        }
        
        if txtFont != nil {
            btnTemp.titleLabel?.font = txtFont
        }
        
        if image != nil {
            btnTemp.setImage(image, for: .normal)
        }
        
        if backgroundImage != nil {
            btnTemp.setBackgroundImage(backgroundImage, for: .normal)
        }
        
        //圆角
        if isRadius {
            btnTemp.layer.borderColor = UIColor.clear.cgColor
            if borderColor != nil {
                btnTemp.layer.borderColor = borderColor!.cgColor
            }
            
            //masksToBounds 会造成离屏渲染
            btnTemp.layer.cornerRadius = cornerRadius!
        }
        
        if borderWidth != nil {
            btnTemp.layer.borderWidth = borderWidth!
        }
        
        return btnTemp
    }
    
    /**!
     * 创建按钮
     * @para rect CGRect
     * @para bottonTitle String 底部标题
     * @para contentImg  UIImage 中间图片
     * @para bgImage     UIImage 背景图(可选)
     */
    static func createButton(rect:CGRect,
                             bottonTitle title:String,
                             titleColor color:UIColor = Setting.K_APP_TINT_COLOR,
                             titleFont font:UIFont = UIFont.systemFont(ofSize: 11),
                             contentImg img:UIImage,
                             bgImage:UIImage?) -> UIButton {
        let _btn = UIButton.init(frame: rect)
        _btn.showsTouchWhenHighlighted = true
        
        //背景图
        if bgImage != nil {
            _btn.setBackgroundImage(bgImage, for: .normal)
        }
        
        //中间图片
        let _y = CGFloat.init(5)
        let _h = CGFloat.init(21)
        let _img:UIImageView = UIImageView.init(image: img)
        _img.frame.origin.y = _y
        _img.frame.origin.x = (rect.size.width - _img.frame.size.width) * 0.5
        _img.frame.size.height = rect.size.height - _h - _y
        
        _img.image = img
        _img.tag = 11
        //imgNoTransformation(img: _img)
        _btn.addSubview(_img)
        
        //底部标题
        let _title:UILabel = UILabel.init(frame: CGRect.init(x: 0,
                                                             y: rect.size.height - _h,
                                                             width: rect.size.width,
                                                             height: _h))
        _title.text = title
        _title.font = font
        _title.textColor = color
        _title.textAlignment = .center
        _btn.addSubview(_title)
        
        return _btn
    }
    
    /**
     * 创建View
     * @return View
     */
    static func createView(rect:CGRect,
                           backgroundColor:UIColor = UIColor.clear,
                           isRadius:Bool = false,
                           cornerRadius:CGFloat = 5,
                           andBorder b:CGFloat = 0,
                           andBorderColor bc:UIColor? = nil) -> UIView {
        
        let view = UIView.init(frame: rect)
        view.backgroundColor = backgroundColor
        
        if isRadius == true {
            //masksToBounds会造成离屏渲染
            view.layer.cornerRadius = cornerRadius
        }
        
        if b > 0 {
            view.layer.borderWidth = b
        }
        
        if bc != nil {
            view.layer.borderColor = bc!.cgColor
        }
        
        return view;
    }
    
    /**
     * 创建图片
     * @return UIImageView
     */
    static func createImage(rect:CGRect,
                            image:UIImage?,
                            backgroundColor:UIColor?,
                            isRadius:Bool = false) -> UIImageView {
        
        let imageView = UIImageView.init(frame: rect)
        
        if backgroundColor != nil {
            imageView.backgroundColor = backgroundColor
        }
        
        if image != nil {
            imageView.image = image
        }
        
        if isRadius == true {
            //masksToBounds会造成离屏渲染
            imageView.layer.cornerRadius = rect.size.height / 2
        }
        
        return imageView
    }
    
    /**
     * 创建Lable
     * @retuen YYLabel
     */
    static func createLable(rect:CGRect,
                            text:String? = nil,
                            textColor:UIColor? = nil,
                            font:UIFont? = nil,
                            backgroundColor:UIColor = UIColor.clear,
                            andTextAlign align:NSTextAlignment = .left) -> YYLabel {
        let labTemp = YYLabel.init(frame: rect)
        labTemp.isOpaque = false
        
        //异步显示
        labTemp.displaysAsynchronously = true
        
        labTemp.backgroundColor = backgroundColor
        labTemp.textAlignment = align
        
        if (text != nil) {
            labTemp.text = text
        }
        
        if font != nil {
            labTemp.font = font
        }
        
        if textColor != nil {
            labTemp.textColor = textColor
        }
        
        return labTemp
    }
    
    /**
     * 创建UITextField
     * @retuen UITextField
     */
    static func createTextFiled(rect:CGRect,
                                textFont:UIFont = UIFont.systemFont(ofSize: 14),
                                textColor:UIColor = UIColor.black,
                                placeHoled:String,
                                borderColor:CGColor? = nil,
                                bottomBoder:UIColor? = nil,
                                placeHoledColor:UIColor = UIColor.gray,
                                placeHoledFont:UIFont = UIFont.systemFont(ofSize: 11),
                                textMarginLeft:CGFloat = CGFloat.init(5),
                                isRadius:Bool = false) -> UITextField {
        
        let txtField = UITextField.init(frame: rect)
        txtField.font = textFont
        
        let str = NSAttributedString(string: placeHoled, attributes: [
            NSAttributedString.Key.foregroundColor:placeHoledColor,
            NSAttributedString.Key.font:placeHoledFont
        ])
        txtField.attributedPlaceholder = str
        
        if borderColor != nil {
            txtField.layer.borderColor = borderColor!
            txtField.layer.borderWidth = 1
        }
        
        if borderColor == nil && bottomBoder != nil {
            let tempRect = CGRect.init(x: 0, y: rect.size.height - 0.3, width: rect.size.width, height: 0.5)
            let labLine = BaseView.createView(rect: tempRect,backgroundColor: bottomBoder!)
            
            txtField.addSubview(labLine)
        }
        
        if isRadius == true {
            //masksToBounds会造成离屏渲染
            txtField.layer.cornerRadius = 5
        }
        
        txtField.leftView = UIView.init(frame: .init(x: 0, y: 0, width: textMarginLeft, height: rect.size.height))
        txtField.leftViewMode = .always
        txtField.textColor = textColor
        
        //清除按钮
        txtField.clearButtonMode = .whileEditing
        
        return txtField
    }
    
    /**
     * 创建UITextView
     * @retuen UITextView
     */
    static func createTxtView(rect:CGRect,
                              textFont:UIFont = UIFont.systemFont(ofSize: 14),
                              borderColor:CGColor? = nil,
                              textColor:UIColor = UIColor.black,
                              backColor:UIColor = UIColor.clear,
                              isRadius:Bool = false,
                              isCanEdit:Bool = true,
                              isShowVScroller:Bool = false,
                              isShowHScroller:Bool = false) -> UITextView {
        
        let txtView = UITextView.init(frame: rect)
        txtView.font = textFont
        txtView.textColor = textColor
        
        if borderColor != nil {
            txtView.layer.borderWidth = 1
            txtView.layer.borderColor = borderColor
        }
        
        txtView.backgroundColor = backColor
        
        if isRadius == true {
            //masksToBounds会造成离屏渲染
            txtView.layer.cornerRadius = 5
        }
        
        txtView.isEditable = isCanEdit
        txtView.showsVerticalScrollIndicator = isShowVScroller
        txtView.showsHorizontalScrollIndicator = isShowHScroller
        
        return txtView
    }
    
    /**
     * 创建UIScrollView
     * @retuen UIScrollView
     */
    static func createScrollView(rect:CGRect,
                                 contentSize:CGSize,
                                 backColor:UIColor = UIColor.clear,
                                 isShowsHorizontalScrollIndicator:Bool = false,
                                 isShowsVerticalScrollIndicator:Bool = false) -> UIScrollView {
        let scrollView = UIScrollView.init(frame: rect)
        scrollView.backgroundColor = backColor
        scrollView.contentSize = contentSize
        
        scrollView.showsHorizontalScrollIndicator = isShowsHorizontalScrollIndicator
        scrollView.showsVerticalScrollIndicator = isShowsVerticalScrollIndicator
        
        return scrollView
    }
    
    
    /// 分段菜单
    /// - Parameters:
    ///   - t: 标题数组([String])
    ///   - tc: 默认字体颜色
    ///   - sc: 选中字体颜色
    ///   - f: 字体
    ///   - bg: 背景色
    static func createSegmentedControlFor(Titles t:[String],
                                          andTintColor tc:UIColor,
                                          andSelectColot sc:UIColor,
                                          andFont f:UIFont,
                                          andBGColor bg:UIColor) -> UISegmentedControl {
        
        let _sw = UISegmentedControl.init(items: t)
        _sw.tintColor = tc
        _sw.selectedSegmentIndex = 0
        
        if #available(iOS 13.0, *) {
            _sw.selectedSegmentTintColor = tc
        }
        
        _sw.setTitleTextAttributes([
            NSAttributedString.Key.font: f,
            NSAttributedString.Key.foregroundColor: tc as Any,
        ], for: .normal)
        
        _sw.setTitleTextAttributes([
            NSAttributedString.Key.font: f,
            NSAttributedString.Key.foregroundColor: sc,
        ], for: .selected)
        
        _sw.backgroundColor = bg
        
        return _sw
    }
    
    /// 创建 UINavigationController
    /// - Parameter vc: UIViewController
    static func createNavFor(ViewController vc:UIViewController) -> UINavigationController {
        let _navigation = UINavigationController.init(rootViewController: vc)
        _navigation.navigationBar.isHidden = true
        _navigation.toolbar.isHidden = true
        
        return _navigation
    }
}

